package com.naiterui.ehp.bs.cms.common.config;

import com.naiterui.ehp.bp.security.utils.SecurityUtils;
import com.naiterui.ehp.bp.support.security.HeaderConstant;
import feign.RequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URLEncoder;

@Slf4j
@Configuration
public class AppConfiguration {

//  @Bean
//  public InterceptorAddition interceptorAddition() {
//    return InterceptorAddition.build(new PasswordExpireInterceptor()).excludePathPatterns("/sys/menu/nav", "/sys/user/info", "/sys/user/password");
//  }

  @Bean
  public RequestInterceptor feignInterceptor() {
    return template -> {
      template.header("X-inner", "1");
      template.header(HeaderConstant.DATA_NO_WRAP, "1");
      try {
        template.header("operator", URLEncoder.encode(SecurityUtils.getCurrentUser().getUsername(), Charsets.UTF_8.name()));
      } catch (Exception ignored) {
        log.warn("增加header失败，获取当前操作人错误");
        template.header("operator", "CMS");
      }
    };
  }

}
